﻿using FakeItEasy;
using MongoDB.Driver;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.ClientDW.Synchronization.SyncHbaseData.Handles;
using Sustainalytics.ClientDW.Synchronization.SyncHbaseData.Helpers;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace GetHbaseDataTests
{
    public class CompanySearchHandleTests
    {
        private readonly string _gaTestConnectionString = ConfigurationManager.ConnectionStrings["MongoDBConnectionString"].ConnectionString;
        private readonly string _dsoTestConnectionString = ConfigurationManager.ConnectionStrings["DSOConnectionString"].ConnectionString;
        private readonly IMongoCollection<CompanySearch> _companySearchCollection;

        public CompanySearchHandleTests()
        {
            var mongoClient = new MongoClient(_gaTestConnectionString);
            var database = mongoClient.GetDatabase(_gaTestConnectionString.Split('/').Last().Trim());
            _companySearchCollection = database.GetCollection<CompanySearch>(typeof(CompanySearch).Name);
        }

        [Fact]
        public async Task IndexCollectionCount_ShouldBeGraterThanOne_AfterIndexRecreation()
        {
            var callbackFake = A.Fake<Action<string>>();
            var generateCompanySearchRecords = A.Fake<GenerateCompanySearchRecords>();
            var companySearchHandle = new CompanySearchHandle(_gaTestConnectionString, _dsoTestConnectionString, generateCompanySearchRecords, callbackFake);

            companySearchHandle.CreateCollectionIndexes().Wait();

            var indexes = await (await _companySearchCollection.Indexes.ListAsync()).ToListAsync();
            Assert.True(indexes.Count > 1);
        }

        [Fact]
        public async Task IndexCollectionCount_ShouldBeEqualToOne_AfterIndexDrop()
        {
            var callbackFake = A.Fake<Action<string>>();
            var generateCompanySearchRecords = A.Fake<GenerateCompanySearchRecords>();
            var companySearchHandle = new CompanySearchHandle(_gaTestConnectionString, _dsoTestConnectionString, generateCompanySearchRecords, callbackFake);

            companySearchHandle.DropCollectionIndexes().Wait();

            var indexes = await (await _companySearchCollection.Indexes.ListAsync()).ToListAsync();
            Assert.True(indexes.Count == 1);
        }
    }
}
